#include "type.h"

/**** 
	1. 2015年 02月 27日 星期五 23:05:12 CST 修复FreeMapEntry的bug,不能判断entry->value_str!=NULL就free
	   因为里面是一个union.
	****/
int SetLongValue(MapEntry *map, char *key, long value)
{
	if (!map || !key) {
		ErrorLog(__FILE__, __LINE__, "arg is null");
		return -1;
	}
	strcpy(map->key, key);	
	map->type = LONGTYPE;
	map->value_l = value;	
	return 0;	
}
int GetLongValue(MapEntry *entry,long *value)
{
	if (value == NULL)
		return -1;
	if (entry->type == LONGTYPE) {
		*value =  entry->value_l;
		return 0;
	}
	else {
		return -2;
	}
}
int SetDoubleValue(MapEntry *map, char *key, double value)
{
	if (!map || !key) {
		ErrorLog(__FILE__, __LINE__, "arg is null");
		return -1;
	}
	
	strcpy(map->key, key);	
	map->type = DOUBLETYPE;
	map->value_d = value;	
	return 0;	
}
int GetDoubleValue(MapEntry *entry, double *value)
{
	if (value == NULL)
		return -1;
	if (entry->type == DOUBLETYPE) {
		*value = entry->value_d;
		return 0;
	}
	else {
		return -2;
	}
}
//必须带入\0
int SetStringValue(MapEntry *map, char *key, char* value)
{
	int r = 0;
	if (!map || !key) {
		ErrorLog(__FILE__, __LINE__, "arg is null");
		return -1;
	}
	int len = strlen(value);	
	strcpy(map->key, key);	
	map->type = STRING;
	if (map->value_str == NULL) {
		map->value_str = InitBuf(len);
		if (map->value_str == NULL) {
			ErrorLog(__FILE__, __LINE__, "get simple buf err");
			return -1;
		}
	}
	r = Setmsg(map->value_str, value, len);
	if (r) {
		ErrorLog(__FILE__, __LINE__, "set msg error");
		return -1;
	}
	return 0;	
}
//不需要带入 \0
int SetStringValueWithLen(MapEntry *map, char *key, char* value, int len)
{
	int r = 0;
	if (!map || !key) {
		ErrorLog(__FILE__, __LINE__, "arg is null");
		return -1;
	}
	strcpy(map->key, key);	
	map->type = STRING;
	if (map->value_str == NULL) {
		map->value_str = InitBuf(len);
		if (map->value_str == NULL) {
			ErrorLog(__FILE__, __LINE__, "get simple buf err");
			return -1;
		}
	}
	r = Setmsg(map->value_str, value, len);
	if (r) {
		ErrorLog(__FILE__, __LINE__, "set msg with len error");
		return -1;
	}
	return 0;	
}
int GetStringValue(MapEntry *entry, char *value, int valuelen)
{
	if (value == NULL || valuelen <= 0)
		return -1;
	if (entry->type == STRING) {
		/* 长度比较大小 */
		int len = entry->value_str->usedlen;
		if (len > valuelen) 
			valuelen = len;
		memcpy(value, entry->value_str->base, valuelen);
		return 0;
	}
	else {
		return -2;
	}
}
/* 先free 然后malloc */
int SetCustomValue(MapEntry *map, char *key, void *value,  int typelen )
{
	int r = 0;
	if (!map || !key) {
		ErrorLog(__FILE__, __LINE__, "arg is null");
		return -1;
	}
	strcpy(map->key, key);	
	map->type = CUSTOM;
	if (map->value_p) {
		free(map->value_p);
	}
	map->value_p = malloc(typelen);
	memcpy(map->value_p, value, typelen);
	return 0;
}
int GetCustomValue(MapEntry *entry, void *value, int typelen)
{
	if (value == NULL)
		return -1;
	if (entry->type == CUSTOM) {
		memcpy(value, entry->value_p, typelen);
		return 0;
	}
	else {
		return -2;
	}
}

void FreeMapEntry(MapEntry *entry)
{
	if (entry == NULL)
		return;
	if (entry->type == STRING) {
		FreeBuf(entry->value_str);
		entry->value_str = NULL;
	}
	if (entry->type == CUSTOM) {
		free(entry->value_p);
		entry->value_p = NULL;
	}
	free(entry);
	entry = NULL;
}
MapEntry* InitMapEntry()
{
	MapEntry* entry = NULL;
	entry = (MapEntry*)malloc(sizeof(MapEntry));
	memset(entry, 0x00, sizeof(MapEntry));
	return entry;
}
int MapEntryCmp(MapEntry *a, MapEntry *b)
{
	if (a == b) 
		return 0;
	if (!a)
		return -1;
	else if (!b)
		return 1;
	return strcmp(a->key, b->key);		
}
int PrintfMapEntry(MapEntry *m)
{
	if (m == NULL)
		return 0;
	if (m->type == STRING) 
		printf("key[%s]-value[%s]\n", m->key, m->value_str->base);
	else if(m->type == LONGTYPE)
		printf("key[%s]-value[%ld]\n", m->key, m->value_l);
	else if(m->type == DOUBLETYPE)
		printf("key[%s]-value[%.2lf]\n", m->key, m->value_d);

	return 0;
}
